# -*- coding: utf-8 -*-

"""
Created on Fri Jun  8 18:43:24 2018

@author: Administrator
"""
#%%

import os
import shutil
import pandas as pd
from ..distribute.poolmap import PoolMap
from ..origin import Shell_script
#%%
class Nii2niigz(Shell_script):
    #作用原理：
    #参数说明：
    #返回值说明：
    #举例：
    #调用：
    #被调用：
    #bug：
    def __init__(self,input_list=[]):
        self.initialize_input_list(input_list)
        self.concat_inputs()
        self.concat_script()
        if os.path.exists(self.out_file_path):
            #if the file is already exist,then there is no neeed to convert or copy the file.s
            self.stdout_err = None
        if self.suffix_gz in self.in_file_path:
            #if the file which the variable 'in_file_path' pointed to is not in nii.gz format,then 
            #it should be converted to nii.gz by the shell command mri_convert
            print('shutil.copy2')
            shutil.copy2(self.in_file_path,self.out_file_path)
            self.stdout_err = None
        else:
            print('self.excute_shell_script')
            self.excute_shell_script()
    
    def initialize_input_list(self,input_list):
        if len(input_list) != 2:
            raise Exception("incompatible parameter amount ")
        self.in_file_path = input_list[0]
        self.out_file_path = input_list[1]
        print('self.in_file_path:',self.in_file_path)
        print('self.out_file_path:',self.out_file_path)
    def concat_script(self):
        ###########################################################################构造命令、即脚本执行。
        #mri_convert -it nii -ot nii  /download/img.nii    /documents/img.nii.gz
        cmd_3 = r' mri_convert -it nii -ot nii ' + self.in_file_path + self.space + self.out_file_path
        self.shell_script = cmd_3

#%%

class Regular2dpabi(object):
  subject_str = 'subject'
  group_str = 'group'
  control_str = 'Control'
  failed_str = 'Failed'
  successed = 'Successed'
  source_filepath_str = 'source_filepath'
  target_filepath_str = 'target_filepath'
  filename_str = 'smoking5.nii'
  source_target_str = 'source_target_str'
  star = '*'
  underline = '_'
  niigz_str = '.nii.gz'
  space = ' '
  def __init__(self,raw_dirpath,excel_filepath,output_dirpath,core_percent):
    excel_df = pd.read_excel(excel_filepath)
    excel_df[self.source_filepath_str] = raw_dirpath + os.sep + excel_df[self.subject_str] + os.sep + self.filename_str
    excel_df[self.target_filepath_str] = output_dirpath + os.sep + excel_df[self.group_str] + self.underline + excel_df[self.subject_str] + self.niigz_str
    excel_df[self.source_target_str] = excel_df.apply(lambda x:[x[self.source_filepath_str],x[self.target_filepath_str]],axis=1)
    
    if not os.path.exists(output_dirpath):
        os.makedirs(output_dirpath)
    
    print('excel_df:',excel_df)
    print('excel_df[self.source_target_str]:',excel_df[self.source_target_str])
    self.poolmap = PoolMap(Nii2niigz,excel_df[self.source_target_str],core_percent)
#    print('poolmap.process_result_list:',poolmap.process_result_list)